package cn.lanqiao.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.ForgetWord;
import cn.lanqiao.entity.RememberWord;
import cn.lanqiao.entity.Word;
import cn.lanqiao.service.WordService;
import cn.lanqiao.service.impl.WordServiceImpl;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	public static int i = 0;
	private WordService ws = new WordServiceImpl();
	private Object[][] tableDatas = null;
	private String[] head = {"单词", "词义"};
	private String[][] datas = null;
	private JButton btnReview;
	private JButton btnAgo;
	private JButton btnSwap;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainFrame() {
		setTitle("一起记单词");
		setIconImage(Toolkit.getDefaultToolkit().getImage("photoes\\tubiao.png"));
		setResizable(false);
		setFont(new Font("微软雅黑", Font.PLAIN, 14));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 573, 456);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setForeground(Color.WHITE);
		menuBar.setEnabled(false);
		menuBar.setBorderPainted(false);
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("菜单");
		menu.setFont(new Font("黑体", Font.PLAIN, 14));
		menuBar.add(menu);
		
		JMenuItem menuItem = new JMenuItem("复习当日");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnReview.doClick();
			}
		});
		menuItem.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menu.add(menuItem);
		
		JMenuItem menuItem_1 = new JMenuItem("复习以前");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnAgo.doClick();
			}
		});
		menuItem_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menu.add(menuItem_1);
		
		JMenuItem menuItem_3 = new JMenuItem("换一组");
		menuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSwap.doClick();
			}
		});
		menuItem_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menu.add(menuItem_3);
		
		JMenuItem menuItem_4 = new JMenuItem("退出");
		menuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		menuItem_4.setFont(new Font("黑体", Font.PLAIN, 14));
		menu.add(menuItem_4);
		
		JMenu menu_2 = new JMenu("账号管理");
		menu_2.setFont(new Font("黑体", Font.PLAIN, 14));
		menuBar.add(menu_2);
		
		JMenuItem menuItem_6 = new JMenuItem("注销用户");
		menuItem_6.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menuItem_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeleteFrm de = new DeleteFrm();
				de.setLocationRelativeTo(null);
				de.setVisible(true);
			}
		});
		menu_2.add(menuItem_6);
		
		JMenuItem menuItem_2 = new JMenuItem("切换用户");
		menuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainFrame.this.dispose();
				UserLoginFrm ulf = new UserLoginFrm();
				ulf.setVisible(true);
				ulf.setLocationRelativeTo(null);
			}
		});
		menuItem_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menu_2.add(menuItem_2);
		
		JMenuItem menuItem_7 = new JMenuItem("取消自动登录");
		menuItem_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				File file = new File("bin\\autoLogin.dat");
				if(file.exists()) {
					file.delete();
					JOptionPane.showMessageDialog(null, "自动登录取消成功！");
				}else {
					JOptionPane.showMessageDialog(null, "自动登录取消失败，未勾选自动登录");
				}
			}
		});
		menuItem_7.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menu_2.add(menuItem_7);
		
		JMenu menu_1 = new JMenu("帮助");
		menu_1.setFont(new Font("黑体", Font.PLAIN, 14));
		menuBar.add(menu_1);
		
		JMenuItem menuItem_5 = new JMenuItem("关于我们");
		menuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AboutWe about = new AboutWe();
				about.setVisible(true);
			}
		});
		menuItem_5.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		menu_1.add(menuItem_5);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnReview = new JButton("复习");
		btnReview.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(i >= getLength()) {
					JOptionPane.showMessageDialog(null, "今日学习任务完成，请休息或者换一组词");
					return;
				}
				WordFrame wordFrame = new WordFrame(table);
				//MainFrame.this.setVisible(false);
				wordFrame.setVisible(true);
			}
		});
		btnReview.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnReview.setBounds(10, 353, 93, 46);
		contentPane.add(btnReview);
		
		btnAgo = new JButton("查看之前");
		btnAgo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(i==0) {
					JOptionPane.showMessageDialog(null, "请先复习");
					return;
				}
				i=0;
				tableDatas = getFWTableDates();
				table.setModel((new DefaultTableModel(tableDatas,head)));
			}
		});
		btnAgo.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnAgo.setBounds(235, 353, 93, 46);
		contentPane.add(btnAgo);
		
		btnSwap = new JButton("换一组");
		btnSwap.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		btnSwap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				i=0;
				tableDatas = getWord();
				table.setModel(new DefaultTableModel(tableDatas,head));
			}
		});
		btnSwap.setBounds(464, 353, 93, 46);
		contentPane.add(btnSwap);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 569, 343);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setSurrendersFocusOnKeystroke(true);
		table.setForeground(new Color(0, 0, 0));
		table.setBackground(new Color(204, 255, 255));
		table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		tableDatas = getWord();
		table.setModel(new DefaultTableModel(tableDatas,head));
		table.getColumnModel().getColumn(0).setPreferredWidth(139);
		table.getColumnModel().getColumn(0).setMinWidth(25);
		table.getColumnModel().getColumn(1).setPreferredWidth(470);
		table.getColumnModel().getColumn(1).setMinWidth(19);
		table.getColumnModel().getColumn(1).setMaxWidth(2147483645);
		scrollPane.setViewportView(table);
		
		JLabel label = new JLabel("学而不思则罔");
		label.setFont(new Font("华文行楷", Font.PLAIN, 18));
		label.setBounds(113, 371, 112, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("思而不学则殆");
		label_1.setFont(new Font("华文行楷", Font.PLAIN, 18));
		label_1.setBounds(338, 371, 116, 15);
		contentPane.add(label_1);
		
		this.setLocationRelativeTo(null);
	}
		
	//获取JTable长度
	public int getLength() {
		return table.getRowCount();
	}
	//取单词表的20个数据，并做成二维数组
	public Object[][] getFWTableDates(){
		datas = new String[20][];
		ForgetWord fw = null;
		RememberWord rw = null;
		for(int i =0;i<15;i++) {
			fw = ws.getForgetWord(UserLoginFrm.account);
			datas[i] = new String [] {fw.getWord(),fw.getTranslation()};
		}
		for(int j =15;j<20;j++) {
			rw = ws.getRememberWord(UserLoginFrm.account);
			datas[j] = new String [] {rw.getWord(),rw.getTranslation()};
		}
		return datas;
	}
	//取总词汇表的数据
	public Object[][] getWord(){
		Object[][] words = new Object[20][];
		Word word = new Word();
		for(int i = 0;i<20;i++) {
			word = ws.getWord();
			words[i] = new String[] {word.getWord(),word.getTranslation()};
		}
		return words;
	}
}
